/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.muqi.app.im.ui;

import java.util.List;

import com.easemob.EMValueCallBack;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.easeui.EaseConstant;
import com.easemob.exceptions.EaseMobException;
import com.muqi.app.im.adapter.GroupAdapter;
import com.muqi.app.qlearn.student.BaseChatActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.widget.pulltorefresh.XListView;
import com.muqi.app.widget.pulltorefresh.XListView.IXListViewListener;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GroupsActivity extends BaseChatActivity implements
		IXListViewListener {
	public static final String TAG = "GroupsActivity";
	private XListView groupListView;
	protected List<EMGroup> grouplist;
	private GroupAdapter groupAdapter;
	public static GroupsActivity instance;

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			groupListView.stopRefresh();
			switch (msg.what) {
			case 0:
				refresh();
				break;
			case 1:
				Toast.makeText(GroupsActivity.this,
						R.string.Failed_to_get_group_chat_information,
						Toast.LENGTH_LONG).show();
				break;

			default:
				break;
			}
		};
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.em_fragment_groups);
		instance = this;
		grouplist = EMGroupManager.getInstance().getAllGroups();

		groupListView = (XListView) findViewById(R.id.list);
		groupListView.setXListViewListener(this);
		groupListView.setPullLoadEnable(false);
		groupListView.setPullRefreshEnable(true);
		groupAdapter = new GroupAdapter(this, 1, grouplist);
		groupListView.setAdapter(groupAdapter);

		groupListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// enter group chat
				Intent intent = new Intent(GroupsActivity.this,
						ChatActivity.class);
				// it is group chat
				intent.putExtra("chatType", EaseConstant.CHATTYPE_GROUP);
				intent.putExtra("userId", grouplist.get(position - 1)
						.getGroupId());
				startActivityForResult(intent, 0);
			}

		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onResume() {
		refresh();
		super.onResume();
	}

	private void refresh() {
		grouplist = EMGroupManager.getInstance().getAllGroups();
		groupAdapter = new GroupAdapter(this, 1, grouplist);
		groupListView.setAdapter(groupAdapter);
		groupAdapter.notifyDataSetChanged();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		instance = null;
	}

	public void back(View view) {
		finish();
	}

	@Override
	public void onRefresh() {
	 EMGroupManager.getInstance().asyncGetGroupsFromServer(new EMValueCallBack<List<EMGroup>>() {
             
             @Override
             public void onSuccess(List<EMGroup> value) {
                 handler.sendEmptyMessage(0);
             }
             
             @Override
             public void onError(int error, String errorMsg) {
                 handler.sendEmptyMessage(1);
             }
         });
	}

	@Override
	public void onLoadMore() {

	}

}
